//
//  UnlockingWayVC.m
//  AILock
//
//  Created by 谭亚明 on 2020/7/16.
//  Copyright © 2020 tym. All rights reserved.
//

#import "UnlockingWayVC.h"
#import "GroupShadowTableView.h"
#import "Sizes.h"
#import "UIButton+Gradient.h"
#import "BangdingEquipUser.h"
#import "LYEmptyViewHeader.h"
#import "TYMEmptyView.h"
#import "EquipSettingVC.h"
#import "EquipSettingVC.h"
#import "UIButton+ImageTitleSpacing.h"
#import "NotBoundUseCell.h"
#import "EquipmentService.h"
@interface UnlockingWayVC ()<GroupShadowTableViewDelegate,GroupShadowTableViewDataSource>

@property(nonatomic,strong) GroupShadowTableView *tableView;
@property(nonatomic,strong) NSMutableArray *dataArray;

@property(nonatomic,strong) EquipmentModel *equipModel;
@property(nonatomic,strong) UserModel *userModel;
@property(nonatomic,assign) NSInteger  typeVC; //1:密码 2:指纹 3:卡
@property(nonatomic,copy) NSString *equipmentUserType;

@property(nonatomic,strong) EquipmentService *services;

@property(nonatomic,weak) IBOutlet UIButton *addPassBtn;
@property(nonatomic,weak) IBOutlet UIButton *bangDingBtn;

@end

@implementation UnlockingWayVC

-(instancetype)initWithType:(NSInteger)type equipMent:(EquipmentModel*)equip userModel:(UserModel*)user{
    self = [super init];
    self.typeVC = type;
    self.equipModel = equip;
    self.userModel = user;
    return self;
}

-(void)viewDidLoad{
    [super viewDidLoad];
    [self addBackBtnFunc];

    [self setTable];
    self.addPassBtn.layer.masksToBounds = YES;
    self.addPassBtn.layer.cornerRadius = 5;
    [self.addPassBtn gradientButtonWithSize:CGSizeMake(300, 44) colorArray:@[(id)rgba(139, 175, 255),(id)rgba(76, 132, 255)] percentageArray:@[@(0.5),@(1)] gradientType:GradientFromLeftTopToRightBottom];
    [self.addPassBtn setImage:[UIImage imageNamed:@"icon_home5"] forState:UIControlStateNormal];
    [self.addPassBtn layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleLeft imageTitleSpace:5];
    
    self.bangDingBtn.layer.masksToBounds = YES;
    self.bangDingBtn.layer.cornerRadius = 5;
    self.bangDingBtn.layer.borderWidth = 1;
    self.bangDingBtn.layer.borderColor = rgba(151, 151, 151).CGColor;
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self getEquipmentusers];
}

-(EquipmentService*)services{
    if (_services == nil) {
        _services = [[EquipmentService alloc] init];
    }
    return _services;
}

//绑定指纹方式
-(IBAction)bangdingAction:(id)sender{
    
    BangdingEquipUser *bangdingUser = [[BangdingEquipUser alloc] init];
    bangdingUser.equipmentUserType = _equipmentUserType;
    bangdingUser.userId = self.userModel.userId;
    bangdingUser.equipmentId = self.equipModel.equipmentId;
    bangdingUser.type = self.userModel.type;
    
    [bangdingUser setVCtitle:self.bangDingBtn.titleLabel.text];
    [self.navigationController pushViewController:bangdingUser animated:YES];
}

//添加指纹or密码or卡    添加
-(IBAction)addPasswordAction:(id)sender{
    
    EquipSettingVC *settingVC = [[EquipSettingVC alloc] init];
    
    settingVC.typeVC = self.typeVC;
    settingVC.equipMentID = self.equipModel.equipmentId;
    settingVC.type = _userModel.type;
    settingVC.userId = _userModel.userId;
    
    [self.navigationController pushViewController:settingVC animated:YES];
}

-(void)getEquipmentusers{
    
    TYMWeak(self);
    [self.services getEquipmentuserList:_equipModel.equipmentId equipmentUserType:_equipmentUserType userId:_userModel.userId type:_userModel.type completion:^(LMJBaseResponse * _Nonnull respons, NSArray<EquipmentUserModel *> * _Nonnull array) {
        if (weakself.dataArray.count) {
            [weakself.dataArray removeAllObjects];
        }
        if(array.count){
            self.tableView.isShowArray = @[@(1)];
            
        }
        [weakself.dataArray addObjectsFromArray:array];
        [weakself.tableView reloadData];
        if(weakself.dataArray.count==0){
            [weakself.tableView ly_showEmptyView];
            self.tableView.isShowArray = @[@(0)];
        }
    }];
}

-(NSMutableArray*)dataArray{
    if (_dataArray == nil) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}

- (void)setTable {
    self.tableView = [[GroupShadowTableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-NavigateonHeight-SafeBottomMargin-75) style:UITableViewStyleGrouped];

    self.tableView.groupShadowDelegate = self;
    self.tableView.groupShadowDataSource = self;
    self.tableView.backgroundColor = viewBGColor;
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 0.01)];
    self.tableView.sectionFooterHeight = 0;
    _tableView.showSeparator = NO;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    
    if (self.typeVC == 1) {
        [self addTitle:@"密码"];
        self.tableView.headTitles = @[@"我的密码"];
        _equipmentUserType = @"02";
        [self.addPassBtn setTitle:@"添加密码" forState:UIControlStateNormal];
        [self.bangDingBtn setTitle:@"绑定已有密码" forState:UIControlStateNormal];
        self.tableView.ly_emptyView = [TYMEmptyView emptyViewWithImageStr:@"empty_img"
         titleStr:@"您还没有添加密码"
        detailStr:@""];
    }else if(self.typeVC == 2){
        [self addTitle:@"指纹"];
        self.tableView.headTitles = @[@"我的指纹"];
        _equipmentUserType = @"00";
        
        [self.addPassBtn setTitle:@"添加指纹" forState:UIControlStateNormal];
        [self.bangDingBtn setTitle:@"绑定已有指纹" forState:UIControlStateNormal];
        self.tableView.ly_emptyView = [TYMEmptyView emptyViewWithImageStr:@"empty_img"
         titleStr:@"您还没有添加指纹"
        detailStr:@""];
    }else if(self.typeVC == 3){
        [self addTitle:@"卡"];
        self.tableView.headTitles = @[@"我的卡"];
        _equipmentUserType = @"01";
        [self.addPassBtn setTitle:@"添加卡" forState:UIControlStateNormal];
        [self.bangDingBtn setTitle:@"绑定已有卡" forState:UIControlStateNormal];
        self.tableView.ly_emptyView = [TYMEmptyView emptyViewWithImageStr:@"empty_img"
         titleStr:@"您还没有添加卡"
        detailStr:@""];
    }else if(self.typeVC == 4){
        [self addTitle:@"遥控"];
        self.tableView.headTitles = @[@"我的遥控"];
        _equipmentUserType = @"03";
        [self.addPassBtn setTitle:@"添加遥控" forState:UIControlStateNormal];
        [self.bangDingBtn setTitle:@"绑定已有遥控" forState:UIControlStateNormal];
        self.tableView.ly_emptyView = [TYMEmptyView emptyViewWithImageStr:@"empty_img"
         titleStr:@"您还没有添加遥控"
        detailStr:@""];
    }
    self.tableView.isShowArray = @[@(1)];
}

#pragma mark delegate datasource
- (NSInteger)numberOfSectionsInGroupShadowTableView:(GroupShadowTableView *)tableView {
    return 1;
}

- (NSInteger)groupShadowTableView:(GroupShadowTableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (CGFloat)groupShadowTableView:(GroupShadowTableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [NotBoundUseCell cellForHeight];
}

- (UITableViewCell *)groupShadowTableView:(GroupShadowTableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NotBoundUseCell *cell = [tableView dequeueReusableCellWithIdentifier:[NotBoundUseCell identifier]];
    if (!cell) {
        cell = [[NotBoundUseCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:[NotBoundUseCell identifier]];
    }
    cell.equUserModel = self.dataArray[indexPath.row];
    return cell;
}

-(void)groupShadowTableView:(GroupShadowTableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    EquipmentUserModel *modl = self.dataArray[indexPath.row];
   
    EquipSettingVC *settingVC = [[EquipSettingVC alloc] initWithEquipmentUserID:modl.id];
    settingVC.oldEquUser = modl;
    [self.navigationController pushViewController:settingVC animated:YES];
}

@end
